<?php
/**
 * School_model.php
 *
 * $Id:$
 */
 
class School_model extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	function get_map_by_project($project_id)
	{
		$list = $this->db
			->from('school')
			->join('project_school', 'school.id=project_school.s_id')
			->where('project_school.p_id', $project_id)
			->where('school.flag', OA_CONST_SCHOOL::FLAG_NORMAL)
			->get()
			->result_array();
		
		if($list == NULL)
		{
			return NULL;
		}
		
		$map = array();
		foreach($list as $school)
		{
			$map[$school['id']] = $school + array(
				'grade_total_num' => 
				$school['grade1_class_num'] + 
				$school['grade2_class_num'] + $school['grade2_class_num']);
		}
		
		return $map;
			
	}
	
	function get_by_id($school_id, $include_deleted = FALSE)
	{
		$query = $this->db
			->from('school')
			->where('school.id', $school_id);
		
		if($include_deleted == FALSE)
		{
			$query->where('school.flag', OA_CONST_SCHOOL::FLAG_NORMAL);
		}

		return $query->get()->row_array();
	}
	
	function get_last_extend_time_by_id($school_id)
	{
		$last_project_school = $this->db
			->select_max('start_time')
			->from('project')
			->join('project_school', 'project.id=project_school.p_id', 'left')
			->where('p_id', $school_id)
			->where('project.flag', OA_CONST_PROJECT::FLAG_NORMAL)
			->get()
			->row_array();
		
		if($last_project_school)
		{
			return $last_project_school['start_time'];
		}
		
		return 0;
	}
	
	function get_list()
	{
		$list = $this->db
			->from('school')
			->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
			->get()
			->result_array();
		
		return $list;
	}
	
}

/* End file of School_model.php */
/* Location: School_model.php */